package com.zq.common.utils.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.common.exception.base.BaseException;
import com.zq.common.utils.StringUtils;

public class DateFormatter {

    // 定义日期时间格式
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 格式化活动时间范围
     *
     * @param startTime 活动开始时间字符串
     * @param endTime 活动结束时间字符串
     * @return 格式化后的时间范围字符串
     */
    public static String formatActivityTime(LocalDateTime startTime, LocalDateTime endTime) {
        // 获取星期几
        String dayOfWeekName = startTime.getDayOfWeek().toString();
        // 构建日期字符串，包括月份和日期
        String formattedDate = startTime.format(DateTimeFormatter.ofPattern("M.dd"));
        // 构建格式化后的字符串
        return startTime.format(DateTimeFormatter.ofPattern("HH:mm")) + "-"
                + endTime.format(DateTimeFormatter.ofPattern("HH:mm")) + " " + getChineseDayOfWeek(dayOfWeekName) + " "
                + formattedDate;
    }

    // 转换英文星期名称为中文
    private static String getChineseDayOfWeek(String dayOfWeekName) {
        switch (dayOfWeekName) {
            case "MONDAY":
                return "周一";
            case "TUESDAY":
                return "周二";
            case "WEDNESDAY":
                return "周三";
            case "THURSDAY":
                return "周四";
            case "FRIDAY":
                return "周五";
            case "SATURDAY":
                return "周六";
            case "SUNDAY":
                return "周日";
            default:
                return "";
        }
    }

    /**
     *
     * @作者 Caige
     * @日期 2024.3.24 18:17
     * @Description //TODO 18:17
     * @param dateRange
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */

    public static Map<String, Object> parseDateRange(String dateRange) {
        Map<String, Object> result = new HashMap<>();
        // 以"周"为分隔符拆分日期范围字符串
        String[] parts = dateRange.split("~");
        if (parts.length == 0) {
            // 如果日期范围为空，则直接返回空结果
            System.out.println("日期范围为空");
            return result;
        }
        // 解析开始日期
        LocalDate startDate = null;
        if (!parts[0].isEmpty()) {
            startDate = parseDate(parts[0]);
        }
        if (startDate != null) {
            result.put("startTime", startDate);
        }
        // 解析结束日期
        LocalDate endDate = null;
        if (parts.length > 1 && !parts[1].isEmpty()) {
            endDate = parseDate(parts[1]);
        }
        if (endDate != null) {
            result.put("endTime", endDate);
        }
        return result;
    }

    private static LocalDate parseDate(String dateString) {
        // 去除日期字符串两端的空格
        dateString = dateString.trim();
        // 获取日期字符串中的日期部分
        String datePart = dateString.split("周")[0];
        System.out.println(datePart);
        // 解析日期
        LocalDate date = null;
        try {
            // 获取当前年份
            int year = LocalDate.now().getYear();
            // 将日期字符串解析为日期对象
            date = LocalDate.parse(year + "-" + datePart, DateTimeFormatter.ofPattern("yyyy-M.d"));
        } catch (Exception e) {
            // 解析失败，输出错误信息
            System.out.println("日期解析失败：" + e.getMessage());
        }
        return date;
    }


    /**
     *
     * @作者 Caige
     * @日期 2024.3.25 15:19
     * @Description //TODO 15:19
     * @param input
     * @return com.zq.common.utils.bo.TimeBo
     */

    public static Map<String, Object> convertDateTimeString(String times) {
        if (StringUtils.isBlank(times)) {
            throw new BaseException("日期解析失败");
        }
        // 初始化ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        String activityTime = null;
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        // 获取当前年份
        int currentYear = now.getYear();
        // 将JSON字符串转换为Data对象数组
        ActivityDateDto[] dataArray = new ActivityDateDto[0];
        try {
            dataArray = objectMapper.readValue(times, ActivityDateDto[].class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        ActivityDateDto startTime = dataArray[0];
        ActivityDateDto endTime = dataArray[1];
        // 判断两天日期是否相等
        String lastTwo = startTime.getMonthAndDay().substring(startTime.getMonthAndDay().length() - 2);
        if (startTime.getDay() == endTime.getDay()) {
            // 在同一天
            activityTime = lastTwo + " " + startTime.getHours() + ":" + startTime.getMinute() + "-" + endTime.getHours()
                + ":" + endTime.getMinute() + "  " + startTime.getMonth() + "." + startTime.getDay();
        } else {
            // 不在同一天
            String lastTwo2 = endTime.getMonthAndDay().substring(endTime.getMonthAndDay().length() - 2);
            activityTime = startTime.getMonth() + "." + startTime.getDay() + " " + startTime.getHours() + ":"
                + startTime.getMinute() + " ~ " + endTime.getMonth() + "." + endTime.getDay() + " " + endTime.getHours()
                + ":" + endTime.getMinute() + "/" + lastTwo + "_" + lastTwo2;
        }
        System.out.println(activityTime);
        LocalDateTime startDateTime = LocalDateTime.of(currentYear, startTime.getMonth(), startTime.getDay(),
            Integer.parseInt(startTime.getHours()), Integer.parseInt(startTime.getMinute()));
        LocalDateTime endDateTime = LocalDateTime.of(currentYear, endTime.getMonth(), endTime.getDay(),
            Integer.parseInt(endTime.getHours()), Integer.parseInt(endTime.getMinute()));
        Map<String, Object> map = new HashMap<>();
        map.put("startTime", startDateTime);
        map.put("endTime", endDateTime);
        map.put("activityTime", activityTime);
        System.out.println(map.get("startTime"));
        System.out.println(map.get("endTime"));
        System.out.println(map.get("activityTime"));
        return map;
    }

}
